package com.itheima.controller;

import com.itheima.aop.annotation.LogOperater;
import com.itheima.pojo.Emp;
import com.itheima.pojo.EmpQueryParam;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.Result;
import com.itheima.service.EmpService;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author：cxl
 * @Package：com.itheima.controller
 * @Project：web-project
 * @name：EmpController
 * @Date：2025-09-26 12:16 pm
 * @Filename：EmpController
 */
@RestController
@Slf4j
@RequestMapping("/emps")

public class EmpController {
    @Autowired
    EmpService empService;

    //修改之前的回显，根据id查询员工
    @LogOperater
    @GetMapping("/{id}")
    public Result selectById(@PathVariable Integer id) {
        log.debug("selectById前端传过来的id={}", id);
        Emp emp = empService.selectById2(id);
        log.info("selectById查询员工{}", emp);
        return Result.success(emp);
    }

    //更新员工信息
    @LogOperater
    @PutMapping
    public Result update(@RequestBody Emp emp) {
        log.info("update前端传过来的员工{}", emp);
        empService.update(emp);
        return Result.success();
    }

    /*
     * 删除员工*/
    @LogOperater
    @DeleteMapping
    //
    public Result delete(@RequestParam List<Integer> ids) {
        //使用数组的话需要用Arrays.toString(ids)才能打印出来
        log.info("删除员工的ids{}", ids);
        empService.delete(ids);


        return Result.success();
    }

    /*
     * 分页条件查询*/
    @GetMapping
    //不要在这里加@RequeryParam
    //？key=value&key2=value2
    //json {key:calue,key2:value2}
    public Result Page(EmpQueryParam empQueryParam) {
        log.debug("ontroller的分页查询入参empQueryParam=\nC" + empQueryParam);
        //调业务层
        PageResult<Emp> pageResult = empService.page(empQueryParam);
        log.debug("EmpController返回结果{}", pageResult);
        return Result.success(pageResult);
    }

    /*新增员工
     * */
    @LogOperater
    @PostMapping
    public Result add(@RequestBody @Valid Emp emp) {
        log.debug("\nController的添加员工入参emp={}", emp);
        // 在 controller 中添加调试代码
        log.info("Received phone number: '{}', length: {}", emp.getPhone(), emp.getPhone().length());
        //调业务层查询
        empService.add(emp);
        //log.debug("\nController的添加员工返回值，生效行数=" + rows);
        return Result.success();

    }

    /*查询所有员工
     * 在班级的添加里可以有下拉框*/
    @LogOperater
    @GetMapping("/list")
    public Result findAll() {
        List<Emp> empList = empService.findAll();
        return Result.success(empList);
    }
}
